package com.school.service.impl;


import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.school.dao.HljtBasicsInfoDao;
import com.school.entity.web.BasicsInfo;
import com.school.service.HljtBasicsInfoService;
import com.school.util.MapperToJson;
import com.school.util.WhetherNotToUpdate;

@Service
public class HljtBasicsInfoServiceImpl implements HljtBasicsInfoService{

	@Autowired
	private HljtBasicsInfoDao oDao;
	@Autowired
	private HljtSchoolServiceImpl hljtSchoolServiceImpl;
	@Autowired
	private HljtSettingServiceImpl hljtSettingServiceImpl;
	@Autowired
	private HljtSchoolimageServiceImpl hljtSchoolimageServiceImpl;
	
	@Override
	public String findBasicsInfo() {
			BasicsInfo oBasicsInfo=	oDao.findBasicsInfo();
			String strPath = null;
			if(null == oBasicsInfo) {
				BasicsInfo g = new BasicsInfo();
				return MapperToJson.toJson(g);
			}
			try {
				strPath= oBasicsInfo.getLogo()!=null&&!oBasicsInfo.getLogo().equals("")? WhetherNotToUpdate.getIp()+oBasicsInfo.getLogo():null;
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		   oBasicsInfo.setLogo(strPath!=null&&!strPath.equals("")? strPath:null);
		   oBasicsInfo.setMonday_Device(
				   oBasicsInfo.getMonday_Device()!=null&&!oBasicsInfo.getMonday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getMonday_Device()):null
				   );
		   oBasicsInfo.setTuesday_Device(
				   oBasicsInfo.getTuesday_Device()!=null&&!oBasicsInfo.getTuesday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getTuesday_Device()):null
				   );
		   oBasicsInfo.setWednesday_Device(
				   oBasicsInfo.getWednesday_Device()!=null&&!oBasicsInfo.getWednesday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getWednesday_Device()):null
				   );
		   oBasicsInfo.setThursday_Device(
				   oBasicsInfo.getThursday_Device()!=null&&!oBasicsInfo.getThursday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getThursday_Device()):null
				   );
		   oBasicsInfo.setFriday_Device(
				   oBasicsInfo.getFriday_Device()!=null&&!oBasicsInfo.getFriday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getFriday_Device()):null
				   );
		   oBasicsInfo.setSaturday_Device(
				   oBasicsInfo.getSaturday_Device()!=null&&!oBasicsInfo.getSaturday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getSaturday_Device()):null
				   );
		   oBasicsInfo.setSunday_Device(
				   oBasicsInfo.getSunday_Device()!=null&&!oBasicsInfo.getSunday_Device().equals("")?
				   MapperToJson.intToBoolean(oBasicsInfo.getSunday_Device()):null
				   );
		   return MapperToJson.toJson(oBasicsInfo);
	}
	@Override
	public String saveBasicsInfo(BasicsInfo oBasicsInfo) {
		Integer schoolall =hljtSchoolServiceImpl.selectall();
		Integer settingall=hljtSettingServiceImpl.findall();
		
		if(schoolall==1&&settingall==1) {
			//修改
			oBasicsInfo.setMonday_Device(MapperToJson.booleanToInt(oBasicsInfo.getMonday_Device()));
		   oBasicsInfo.setTuesday_Device(MapperToJson.booleanToInt(oBasicsInfo.getTuesday_Device()));
		   oBasicsInfo.setWednesday_Device(MapperToJson.booleanToInt(oBasicsInfo.getWednesday_Device()));
		   oBasicsInfo.setThursday_Device(MapperToJson.booleanToInt(oBasicsInfo.getThursday_Device()));
		   oBasicsInfo.setFriday_Device(MapperToJson.booleanToInt(oBasicsInfo.getFriday_Device()));
		   oBasicsInfo.setSaturday_Device(MapperToJson.booleanToInt(oBasicsInfo.getSaturday_Device()));
		   oBasicsInfo.setSunday_Device(MapperToJson.booleanToInt(oBasicsInfo.getSunday_Device()));
			hljtSchoolServiceImpl.setSchool(oBasicsInfo);
			
			hljtSettingServiceImpl.setSetting(oBasicsInfo);

			
			
		return "true";
		}else {
		//添加
		hljtSchoolimageServiceImpl.deleteimage(1);
		hljtSchoolServiceImpl.deleteall();
		hljtSettingServiceImpl.deleteSetting();
		oBasicsInfo.setMonday_Device(MapperToJson.booleanToInt(oBasicsInfo.getMonday_Device()));
		   oBasicsInfo.setTuesday_Device(MapperToJson.booleanToInt(oBasicsInfo.getTuesday_Device()));
		   oBasicsInfo.setWednesday_Device(MapperToJson.booleanToInt(oBasicsInfo.getWednesday_Device()));
		   oBasicsInfo.setThursday_Device(MapperToJson.booleanToInt(oBasicsInfo.getThursday_Device()));
		   oBasicsInfo.setFriday_Device(MapperToJson.booleanToInt(oBasicsInfo.getFriday_Device()));
		   oBasicsInfo.setSaturday_Device(MapperToJson.booleanToInt(oBasicsInfo.getSaturday_Device()));
		   oBasicsInfo.setSunday_Device(MapperToJson.booleanToInt(oBasicsInfo.getSunday_Device()));
		
		
		hljtSchoolServiceImpl.addSchool(oBasicsInfo);
		
		hljtSettingServiceImpl.addSetting(oBasicsInfo);
		
		return "true";
		}
	}
/*	@Override
	public String findSchoollInfo(String strPath) {
		HljtSchoolSynopsis oHljtSchoolSynopsis=oDao.findSchoolInfo("1");
		strPath=strPath+"/myimages/schools/"+oHljtSchoolSynopsis.getImagePath();
		oHljtSchoolSynopsis.setImagePath(strPath);
		return MapperToJson.toJson(oHljtSchoolSynopsis);
	}
	@Override
	public String saveSchoollInfo(HljtSchoolSynopsis oHljtSchoolSynopsis){
		int nCount= oDao.saveSchoolInfo(oHljtSchoolSynopsis);
		if(nCount!=0){
			WhetherNotToUpdate.setPattern(true);
			return "true";
		}
		return "false";
	}*/
}
